// top module of keyboard in to LCD display out

module lcdtop(
		displaymode, keydata, editphrase, entry, acceptguesses, phrasestarted, gameoutcome,
		rst, clock50,
		keyboard_clk, keyboard_data,
		LCD_DATA, LCD_ON, LCD_BLON, LCD_RW, LCD_EN, LCD_RS,
		phrase0, phrase1, hex2, hex3, hex4, hex5, hex6, hex7, diekey
);

	// Inputs/outputs
			// System inputs
			input clock50;
			input rst;
			input[3:0] displaymode;
			inout[127:0] phrase0;
			inout[127:0] phrase1;
			input[1:0] editphrase;
			input[15:0] entry;
			input[1:0] acceptguesses;
			output phrasestarted;
			output[6:0] hex2;
			output[6:0] hex3;
			output[6:0] hex4;
			output[6:0] hex5;
			output[6:0] hex6;
			output[6:0] hex7;
			output[1:0] gameoutcome;
			output diekey;
			
			// Keyboard inputs
			input keyboard_clk;
			input keyboard_data;

			// 16x2 LCD display outputs
			output[7:0] LCD_DATA;
			output LCD_ON; // LCD Power ON/OFF
			output LCD_BLON;  // LCD Backlight ON/OFF
			output LCD_RW; // LCD Read/Write Select, 0 = Write, 1 = Read
			output LCD_EN; // LCD Enable
			output LCD_RS; // LCD Command/Data Select, 0 = Command, 1 = Data
	
	// regs/wires used in modules
			//wire [127:0] line0; // character data for lcd module line 0
			wire [127:0] line0; // character data for lcd module line 0
			wire [127:0] line1; // character data for lcd module line 1
			wire reset = 1'b0; // used in keyboard
			wire read, scan_ready; // used for keyboard input
			wire [7:0] scan_code; // keyboard code input, passed into keyhistory
			reg [7:0] keyhistory[1:2]; // keyboard last two codes, passed into keydata
			inout[16:0] keydata;
			reg [16:0] keydata; // *** important *** 8-BIT KEYBOARD DATA *** important ***
			wire [7:0] lcdkey; // keyboard data after conversion for lcd
			wire validkey;
			wire [15:0] whoshome0;
			wire [15:0] whoshome1;
			
// keyboard modules -- read scan_code, output keydata[15:0]
	keyboard kbd(
	  .keyboard_clk(keyboard_clk),
	  .keyboard_data(keyboard_data),
	  .clock50(clock50),
	  .reset(reset),
	  .read(read),
	  .scan_ready(scan_ready),
	  .scan_code(scan_code)
	);
	oneshot pulser(
		.pulse_out(read),
		.trigger_in(scan_ready),
		.clk(clock50)
	);
	always @(posedge scan_ready)
	begin
		keyhistory[2] <= keyhistory[1];
		keyhistory[1] <= scan_code;
	end
	
	reg[15:0] lastclkkey;
	reg[15:0] currentclkkey;
	always@(posedge clock50)
	begin
		lastclkkey <= currentclkkey;
		currentclkkey <= keydata;
	end
	
	always @(*)
	begin
		if(lastclkkey == currentclkkey)
			keydata[16] = 1'b0;
		else keydata[16] = 1'b1;
		
		keydata[15:8] = keyhistory[2];
		keydata[7:0] = keyhistory[1];
	end
// end keyboard modules
	
// converts keyboard input(keydata) to lcd display-compatable output(lcdKey)
	omg look(keydata, validkey, lcdkey, ledg); 
	
	updatelines update(lcdkey, line0, line1, validkey, displaymode, phrase0, phrase1, phrasestarted, whoshome0, whoshome1);
	
<<<<<<< .mine
	editphrase ep(editphrase, phrasestarted, phrase0, phrase1, entry, keydata, clock50);
=======
	editphrase ep(editphrase, phrase0, phrase1, entry, keydata, hex2, hex3, hex4, hex5, hex6, hex7);
>>>>>>> .r129
	
<<<<<<< .mine
	guessing gp(acceptguesses, phrase0, phrase1, entry, keydata, clock50, hex2, hex3, hex4, hex5, hex6, hex7, whoshome0, whoshome1, gameoutcome, diekey);
	
=======
	guessing gs(guessstate, phrase0, phrase1, entry, keydata, guess);
	
>>>>>>> .r129
// displays line0 and line1 to LCD
	lcdcontrol lcd(
		.CLOCK_50(clock50), .RST(rst), .LCD_ON(LCD_ON),
		.LCD_BLON(LCD_BLON), .LCD_RW(LCD_RW), .LCD_EN(LCD_EN), .LCD_RS(LCD_RS),
		.LCD_DATA(LCD_DATA), .line0(line0), .line1(line1)
	);
// end lcd display
	
endmodule